$(document).ready(function(){
		initData(1);
		//jquery 限制只能输入数字
		$("#goPage").keyup(function(){
			 $(this).val($(this).val().replace(/\D|^0/g,''));  
		 }).bind("paste",function(){  //CTR+V事件处理     
			 $(this).val($(this).val().replace(/\D|^0/g,''));     
		 }).css("ime-mode", "disabled"); //CSS设置输入法不可用  
});

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

//列表显示
function initData(currentPage){
	var term = $("#term").val();
	var subject = $("#subject").val();
	var grade = $("#grade").val();
	var level = $("#level").val();
	
	var path = $("#path").val()+"/Pagination.action";
	var pageSize = 10;
	var pageCount = "";
   	$.ajax({
	      type: "post",
	      url: path,
	      dataType: "json",
	      data: "currentPage="+currentPage
	      +"&pageSize="+pageSize
	      +"&term="+encodeURI(encodeURI(term))
	      +"&subject="+encodeURI(encodeURI(subject))
	      +"&grade="+encodeURI(encodeURI(grade))
	      +"&level="+encodeURI(encodeURI(level)),
	      
	      beforeSend: function(){$("#divload").show();$("#divshow").hide();},//发送数据之前
	      complete: function(){$("#divshow").show();$("#divload").hide();},//发送数据之前
	      success: function(json){
	    	  $("#myTable tr:gt(0)").remove();//移除Id为result的表格里的行，从第二行开始
	    	  //总记录数
	    	  pageCount = json[0].pageCount;
		    	 if(pageCount != 0){
			          //遍历取数据
			    	  for(var i = 0; i < json.length; i++){
		    			  $("#dataShow").append("<tr>"+
					          "<td>"+json[i].year+"</td>"+
					          "<td>"+json[i].term+"</td>"+
					          "<td>"+json[i].subName+"</td>"+
					          "<td>"+json[i].graName+"</td>"+
					          "<td>"+json[i].claLevel+"</td>"+
					          "<td>"+json[i].claNum+"</td>"+
					          "<td>"+json[i].perunitFee+"</td>"+
					          "<td>"+json[i].perunitFee*json[i].claNum+"</td>"+
					          "<td>"+json[i].bookFee+"</td>"+
					          "<td>"+json[i].predictNum+"</td>"+
					          "<td>"+json[i].claType+"</td>"+
					          "<td>"+json[i].bookVersion+"</td>"+
					          "<td><a href='javascript:void(0);' onclick='openWins("+json[i].couId+","+json[i].claNum+");'>添加</a>" +
					          "&nbsp;<a href='javascript:void(0);' onclick='delCourse("+json[i].couId+");'>删除</a>" +
					          "&nbsp;<a href='javascript:void(0);' onclick='upCourse("+json[i].couId+");'>修改</a>" +
					          "&nbsp;<a href='javascript:void(0);' onclick='openDisplay("+json[i].couId+","+json[i].claNum+");'>查看</a></td>"+
					        "</tr>");
			    	  }
			    	  //行交替颜色
			    	  $("#myTable tr:even").css("background-color","#f5f5f5");
				  		$("#myTable tr").live("mouseenter",function(){
				  			$(this).addClass("lightbluebg");
				  		}).live("mouseleave",function(){
				  			$(this).removeClass("lightbluebg");
				  		});
		    	  }else{
		    		  $.dialog.tips('没有匹配的数据！',1,'alert.gif');
		    		  $("#dataShow").append("<tr><td colspan='13'>没有匹配的数据！</td></tr>");
		    	  }
	    	  
		    	  //漂亮的提示 哈哈
//		    	  $('.skyblue').poshytip({
//		    			className: 'tip-skyblue',
//		    			bgImageFrameSize: 9,
//		    			offsetX: 0,
//		    			offsetY: 20
//		    	  });
		    	 
		    	//设定分页
		    	    $("#totalRows").html(pageCount);//总记录数
		    	    $("#currentPage").html(currentPage);//当前页数
		    	    $("#pageSize").html(pageSize); //每页显示记录数
		    	    
		    	    var totalPageNum = 0;
		    	    if((pageCount % pageSize) == 0){
			    	     $("#totalPages").html(pageCount / pageSize);
			    	     totalPageNum = pageCount / pageSize;
			    	     
		    	    }else{
			    	     $("#totalPages").html(parseInt(pageCount / pageSize) + 1);
			    	     totalPageNum = parseInt(pageCount / pageSize) + 1;
		    	    }
		    	    $("#totalPge").val(totalPageNum); //将总页数赋值给totalPge隐藏域
		    	    //设定上一页，下一页  首页、尾页的链接
		    	    if(pageCount <= pageSize){
		    	    	 $("#first").attr("href","javascript:void(0);");
		    	    	 $("#previous").attr("href","javascript:void(0);");
		    	    	 $("#next").attr("href","javascript:void(0);");
		    	    	 $("#last").attr("href","javascript:void(0);");
		    	    	 $("#go").removeAttr("onclick");
		    	    }else if(currentPage == 1){
		    	    	 $("#first").attr("href","javascript:void(0);");
		    	    	 $("#previous").attr("href","javascript:void(0);");
		    	    	 $("#next").attr("href","javascript:initData("+(currentPage+1)+");");
		    	    	 $("#last").attr("href","javascript:initData("+totalPageNum+");");
		    	    }else if(currentPage == totalPageNum){
		    	    	 $("#first").attr("href","javascript:initData(1);");
		    	    	 $("#previous").attr("href","javascript:initData("+(currentPage-1)+");");
		    	    	 $("#next").attr("href","javascript:void(0);");
		    	    	 $("#last").attr("href","javascript:void(0);");
		    	    }else{
		    	    	 $("#first").attr("href","javascript:initData(1);");
		    	    	 $("#previous").attr("href","javascript:initData("+(currentPage-1)+");");
		    	    	 $("#next").attr("href","javascript:initData("+(currentPage+1)+");");
		    	    	 $("#last").attr("href","javascript:initData("+totalPageNum+");");
		    	    }
		    	    
	      }//success END
	}); //ajax  END
}//分页结束

//输入页数跳转页面
function goOnclick(){
	var pageNum = $("#goPage").val();
	var totalPge = $("#totalPge").val();
	if(pageNum != "" && parseInt(pageNum) <= parseInt(totalPge)){
		initData(pageNum);
	}else{
		$.dialog.tips('请输入总页数以内的整数！',3,'alert.gif');
	}
}

//弹窗添加课程大纲
function openWins(id,claNum){
	var path = $("#path").val()+'/win/addCourse.jsp?id='+id+'&claNum='+claNum;
    $.dialog({
    	lock:true,
    	fixed:true,
    	title:'新增课程大纲&nbsp;&nbsp;&nbsp;&nbsp;共'+claNum+'次课',
    	width:'800px',
		height:500,
		content:'url:'+path
//    	close: function(){
//	        this.reload();  //关闭窗口时刷新主页面
//	    },
//    	cancelVal: '关闭',
//		cancel: true
//			function(){
//	        this.reload();  //关闭窗口时刷新主页面
//	    }
    });
}
//显示课程大纲
function openDisplay(id,claNum){
	var path = $("#path").val()+'/win/list.jsp?id='+id+'&isOut=0';
    $.dialog({
//    	lock:true,
    	fixed:true,
    	title:'课程大纲详细&nbsp;&nbsp;&nbsp;&nbsp;共'+claNum+'次课',
    	width:'880px',
		height:500,
    	content:'url:'+path,
    	cancelVal:'关闭',
		cancel:true,
		close: function(){
	       //window.parent.opener=null;//如果不加这句，会提示关闭询问窗口; 
           this.reload(); 
           }
    });
}

//修改课程设置
function upCourse(id){
	var path = $("#path").val()+'/InitQuery.action?id='+id;
	window.location.href=path;
	
	
}
function upCourseReal(id,calNum)
{
	var path = $("#path").val()+'/win/modifyCourse.jsp?id='+id+'&claNum='+claNum;
    $.dialog({
    	lock:true,
    	fixed:true,
    	title:'修改课程大纲第'+claNum+'次课',
    	width:'880px',
		height:500,
    	content:'url:'+path,   
    	close: function(){
	       //window.parent.opener=null;//如果不加这句，会提示关闭询问窗口; 
           this.reload(); 
           }
//    	cancelVal:'关闭',
//		cancel:true
    });
}
/*删除课程介绍*/
function delCourse(id){
	$.dialog.confirm('这会将课程大纲一并删除，你确定要删除吗？', function(){
		var path = $("#path").val()+ "/DelCourse.action";
		$.ajax({
		      type: "post",
		      url: path,
		      cache: false,
		      dataType: "json",
		      data: "id="+id,
		      
		      //发送数据之前
		      beforeSend: function(){$.dialog.tips('正在删除数据...',3,'loading.gif');},
		      //complete: function(){},
		      success: function(result){
		    	  if(result[0].flag == 1){
		    		  $.dialog.tips('删除成功！',3,'success.gif');
		    		  initData();
		    	  }else{
		    		  $.dialog.tips('您好，删除失败，请及时联系管理员处理！',3,'error.gif');
		    	  }
		      }
		});
	}, function(){
	    $.dialog.tips('已取消！',1,'confirm.gif');
	});
}

